﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace ReagentStore.UTL
{
	public class DESEncryptor
	{
		private const string _key = "59761842";
		private static readonly byte[] _vector = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};

		public static string Encrypt(string encryptString)
		{
			try
			{
				byte[] rgbKey = Encoding.UTF8.GetBytes(_key.Substring(0, 8));
				byte[] rgbIV = _vector;
				byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
				var dCSP = new DESCryptoServiceProvider();
				var mStream = new MemoryStream();
				var cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
				cStream.Write(inputByteArray, 0, inputByteArray.Length);
				cStream.FlushFinalBlock();

				return Convert.ToBase64String(mStream.ToArray());
			}
			catch
			{
				return encryptString;
			}
		}
	}
}